import 'package:flutter/material.dart';

class SessionConfigWidget extends StatelessWidget {
  const SessionConfigWidget({
    super.key,
    required this.formKey,
    required this.nameInputController,
    required this.hostInputControllers,
    required this.portInputController,
  });
  final GlobalKey<FormState> formKey;
  final TextEditingController nameInputController;
  final List<TextEditingController> hostInputControllers;
  final TextEditingController portInputController;

  @override
  Widget build(BuildContext context) {
    return Form(
      key: formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextFormField(
            controller: nameInputController,
            maxLength: 128,
            decoration: const InputDecoration(
              border: UnderlineInputBorder(),
              labelText: "会话名称",
            ),
          ),
          const Padding(
            padding: EdgeInsets.symmetric(vertical: 6.0),
            child: Text("主机IP地址"),
          ),
          Row(
            children: List.generate(
              4,
              (index) => Expanded(
                flex: 1,
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 2),
                  child: TextFormField(
                    controller: hostInputControllers[index],
                    validator: (value) {
                      if (value == null) return "IP地址不完整";
                      try {
                        int blank = int.parse(value);
                        if (blank < 0 || blank > 255) {
                          return "请输入0到255之间的数字";
                        }
                      } on FormatException {
                        return "IP地址中存在非法字符";
                      }
                      return null;
                    },
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                    ),
                  ),
                ),
              ),
            ),
          ),
          TextFormField(
            controller: portInputController,
            validator: (value) {
              if (value == null) return "请输入端口号";
              try {
                int blank = int.parse(value);
                if (blank < 0 || blank > 65535) return "请输入0到65535之间的数字";
              } on FormatException {
                return "端口号中存在非法字符";
              }
              return null;
            },
            decoration: const InputDecoration(
              border: UnderlineInputBorder(),
              labelText: "端口号",
            ),
          ),
        ],
      ),
    );
  }
}
